package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.api.security.ShiroUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.domain.entity.SysRole;
import cn.topcodes.tcsf.admin.domain.form.SysRoleForm;
import cn.topcodes.tcsf.admin.domain.view.SysPermVo;
import cn.topcodes.tcsf.admin.service.res.SysPermService;
import cn.topcodes.tcsf.admin.service.org.SysRoleService;
import cn.topcodes.tcsf.admin.service.org.SysUserService;
import cn.topcodes.tcsf.admin.web.controller.SysRoleApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统角色管理控制器
 * @author Unicorn
 */
@Controller
@RequestMapping("/admin/sys-role")
public class SysRoleAdminController extends AdminController<SysRole, SysRole, SysRoleForm> implements SysRoleApi {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysPermService sysPermService;

	@Autowired
	private SysUserService sysUserService;

	@Override
	protected String getPageFolder() {
		return "admin/role";
	}

	/*系统角色权限分配页面*/
	@RequestMapping(value = "assign-perm",method = RequestMethod.GET)
	public String assignPerm(@RequestParam Long id,Model model) {
		SysRole role = this.sysUserService.getRole(ShiroUtil.getUserId());
		List<SysPermVo> perms = this.sysPermService.getPerms(role.getId(),id, ShiroUtil.getUserId());
		model.addAttribute("id",id);
		model.addAttribute("perms",perms);
		return "admin/role/assign-perm";
	}

	/*系统角色权限分配提交接口*/
	@ResponseBody
	@RequestMapping(value = "assign-perm",method = RequestMethod.POST)
	public ApiResponseEntity<Void> assignPerm(@RequestParam Long id, @RequestParam(name = "permIds",required = false) Long[] ids) {
		this.sysRoleService.assignPerm(id,ids);
		return ApiResponseEntity.buildOK();
	}

}
